package com.tucanstubs.android_wheresmystuff.activities;


import java.util.List;
import com.tucanstubs.android_wheresmystuff.R;
import com.tucanstubs.android_wheresmystuff.database.UserHelper;
import com.tucanstubs.android_wheresmystuff.items.Item;
import com.tucanstubs.android_wheresmystuff.login.User;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

/**
 * This Activity is used to display a users home screen. This screen is used for
 * the home screen of all user's both admins and non-admins.
 * From here the user can select multiple operations to perform
 * @author team 37
 * @version 2.0 4/9/2013
 *
 */
public class HomeScreenActivity extends Activity {
	
	private Button usersButton;
	private User user;
	
	
	/**
	 * Creates the activity. Initializes all GUI Components
	 *  and retrieves information form the bundle.
	 * The activity is passed a bundle with the key Tags.USERNAME mapping to
	 * the user name of the currently logged in user.
	 * 
	 * @param SavedInstanceState not used
	 */
	public void onCreate(Bundle SavedInstanceState){
		super.onCreate(SavedInstanceState);
		setContentView(R.layout.home_screen);
		usersButton = (Button) findViewById(R.id.User_List_Button);
		Bundle bundle = getIntent().getExtras();
		String userName = bundle.getString(Tags.USERNAME);
		user = UserHelper.get(this, userName);
		//Checks to see if the user is not an admin
		// if user is not an admin then the users button disappears
		if(!user.isAdmin()){		
			Log.i(Tags.HOMESCREENACTIVITY, "Users is invisible");
			usersButton.setVisibility(View.GONE);
		}
		Log.i(Tags.HOMESCREENACTIVITY,userName);
	}
	
	/**
	 * Method that is called when a button is clicked. Transfers control to the next Activity based
	 * on which button was clicked
	 * 
	 * @param view The Button which was clicked on
	 */
	public void onClick(View view) {
		Intent nextIntent;
		Bundle bundle=new Bundle();
		switch(view.getId()) {
		//find out which button was clicked and transfer control to the
		//corresponding Activity
		//also put appropriate information in the bundle
		case R.id.Add_Item_Button:
			
			nextIntent = new Intent(this,AddItemActivity.class);
			bundle.putString(Tags.USERNAME, user.getUserName());
			nextIntent.putExtras(bundle);
			startActivity(nextIntent);
			break;
		case R.id.Search_Item_Button:
			nextIntent= new Intent(this,SearchItemActivity.class);
			bundle.putInt(Tags.ITEM,-1);
			bundle.putString(Tags.USERNAME,user.getUserName());
			nextIntent.putExtras(bundle);
			startActivity(nextIntent);
			break;
		case R.id.My_Items_Button:
			nextIntent = new Intent(this,ItemListActivity.class); 
			List<Item> itemsList = user.getItems();
			Log.i(Tags.HOMESCREENACTIVITY,itemsList.toString());
			int[] itemIDs = new int[itemsList.size()];
			//make the array of item ids
			int j = 0;
			for (Item i: itemsList) 
				itemIDs[j++] = i.getId();
			bundle.putIntArray(Tags.ITEMIDS, itemIDs);
			bundle.putString(Tags.USERNAME, user.getUserName());
			bundle.putString(Tags.CLASS, Tags.HOMESCREENACTIVITY);
			nextIntent.putExtras(bundle);
			startActivity(nextIntent);
			break;
		case R.id.User_List_Button:
			nextIntent = new Intent(this,UserListActivity.class);
			bundle.putString(Tags.USERNAME, user.getUserName());
			nextIntent.putExtras(bundle);
			startActivity(nextIntent);
			break;
		case R.id.Log_Out_Button:
			nextIntent = new Intent(this,LoginPageActivity.class);
			startActivity(nextIntent);
			Toast toast= Toast.makeText(getApplicationContext(), 
					"It's not ogre, until I say it's ogre!", Toast.LENGTH_LONG);  
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
			finish();
			break;
		}
		
	}
}
